package com.cooper.java.scraper;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: cooperlee
 * Date: 11/15/12
 * Time: 9:40 AM
 */
public class Scraper {

    public static boolean parseBool(String value, boolean defaultValue) {
        if (value == null || "".equals(value.trim())) {
            return defaultValue;
        } else
            return value.trim().equals("yes");
    }

    public Scraper(File scraperFile,DownloadParams downloadParams) {
        this.downloadParams = downloadParams;
        SAXReader reader = new SAXReader();
        try {
            document = reader.read(scraperFile);
            clearBuffer();
        } catch (DocumentException e) {
            e.printStackTrace();
        }
    }

    public Scraper(Document document, DownloadParams downloadParams){
        this.document = document;
        this.downloadParams = downloadParams;
        clearBuffer();
    }

    public void clearBuffer() {
        for (int i = 0; i < buffer.length; i++) {
            buffer[i] = "";
        }
    }

    protected Document document;
    protected DownloadParams downloadParams;

    private String[] buffer = new String[21];

    public Scraper clonScraper() {
        return new Scraper(document, downloadParams);
    }

    protected void setBufferValue(int index, String value) {
        buffer[index] = (value == null ? "" : value);
    }

    String[] getBuffer() {
        return buffer;
    }

    private HttpDownload httpDownload = null;

    HttpDownload getHttpDownload() {
        if (httpDownload == null) {
            httpDownload = new HttpDownload(downloadParams);
        }
        return httpDownload;
    }


    protected DefineTag getDefineTag(String tagName) {
        return DefineTag.defineTagFactory(this,
                document.getRootElement(), tagName);
    }

    /*

    protected Map<String, String> getResultParam(String xml, String keyTagName,
                                                 String keyAttrName){
        Map<String, String> result = new HashMap<String, String>();
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(xml));
        int eventType = xpp.getEventType();
        boolean inSortTag = false;
        String key = "";
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {
                if (keyTagName.equals(xpp.getName())) {
                    inSortTag = true;
                    key = xpp.getAttributeValue(null, keyAttrName);
                }
            } else if (eventType == XmlPullParser.TEXT) {
                if (inSortTag) {
                    result.put(key, xpp.getText());
                }
            } else if (eventType == XmlPullParser.END_TAG) {
                if (keyTagName.equals(xpp.getName())) {
                    inSortTag = false;
                }
            }
            eventType = xpp.next();
        }
        return result;
    }   */

    @Override
    public boolean equals(Object object) {
        if (object == null) {
            return false;
        }
        if (this == object) {
            return true;
        }
        if (object instanceof Scraper) {
            return ((Scraper) object).document.equals(document);
        } else
            return false;
    }

}
